<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="活动类型" prop="imgType">
        <el-select
          v-model="queryParams.imgType"
          placeholder="活动类型"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="item in proTypeOptions"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <!-- <el-button icon="Refresh" @click="resetQuery">重置</el-button> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="noticeId" width="100">
        <template #default="scope">
          <span>{{
            scope.$index + 1 + (queryParams.page - 1) * queryParams.pageSize
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缩略图" align="center" prop="imgUrl" width="250">
        <template #default="scope">
          <div
            class="table-img"
            :style="`background-image: url('${scope.row.imgUrl}')`"
          ></div>
        </template>
      </el-table-column>
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="活动类型"
        align="center"
        prop="noticeType"
        width="100"
      >
        <template #default="scope">
          {{ typeFilter(scope.row.proType) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag :options="sys_notice_status" :value="scope.row.status" />
        </template>
      </el-table-column> -->
      <el-table-column
        label="创建者"
        align="center"
        prop="createBy"
        width="100"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <BannerDialog
      v-model="dialogVisible"
      :currentBanner="currentBanner"
      @confirm="submit"
    ></BannerDialog>
  </div>
</template>

<script setup name="Banner">
import {
  addBanner,
  getBannerList,
  delBanner,
  getActivityTypeBanner,
  getBannerById,
  updateBanner,
} from "@/api/system/banner";
import BannerDialog from "./components/BannerDialog.vue";
import useActivityStore from "@/store/modules/activity";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
let dialogVisible = ref(false);
const proTypeOptions = ref([]);
const currentBanner = ref({});

const data = reactive({
  formData: {},
  queryParams: {
    imgType: "",
    page: 1,
    pageSize: 10,
  },
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, formData, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  getBannerList(queryParams.value).then((response) => {
    noticeList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  formData.value = {};
  proxy.resetForm("formRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  currentBanner.value = {};
  dialogVisible.value = true;
  title.value = "添加轮播图";
}

/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  if (row) {
   currentBanner.value = JSON.parse(JSON.stringify(row));
  }
  dialogVisible.value = true;
  title.value = "修改轮播图";
}

function submit(formData) {
  if (!currentBanner.value.id) {
    addBanner(formData).then((response) => {
      getList();
      proxy.$modal.msgSuccess("增加成功");
    });
  } else {
    updateBanner({ ...formData, id: currentBanner.value.id }).then(
      (response) => {
        getList();
        proxy.$modal.msgSuccess("修改成功");
      }
    );
  }
}

function updateForm(formData) {
  updateBanner(formData).then((response) => {
    if (response.code == 200) {
      getList();
      proxy.$modal.msgSuccess("保存成功");
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const bannerIds = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除？")
    .then(function () {
      return delBanner(bannerIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

async function getBannerType() {
  await getActivityTypeBanner().then((res) => {
    proTypeOptions.value = res.data.records;
  });
}
onMounted(async () => {
  // proTypeOptions.value = await useActivityStore().getType();
  await getBannerType();
  queryParams.value.imgType = proTypeOptions.value[0].id;
  getList();
});
</script>
<style scoped>
.table-img {
  width: 220px;
  height: 80px;
  border-radius: 5px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center center;
}
</style>